package com.gky.gkytemplate.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        // 设置 key 的序列化器为 StringRedisSerializer，确保 key 是字符串形式
        template.setKeySerializer(new StringRedisSerializer());
        // 设置 value 的序列化器为 GenericJackson2JsonRedisSerializer，以 JSON 格式存储 value
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }

}
